package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.pay.dto.MerchantSystemPageQuery;
import com.eastfair.pay.dto.MerchantSystemSaveDTO;
import com.eastfair.pay.dto.MerchantSystemUpdateDTO;
import com.eastfair.pay.entity.MerchantSystem;
import com.eastfair.pay.service.MerchantSystemService;
import com.eastfair.pay.vo.MerchantSystemVO;
import com.eastfair.projectcore.enumeration.MenuAffiliationEnum;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 商户系统
 * </p>
 *
 * @author clm
 * @date 2023-02-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/merchantSystem")
@Api(value = "MerchantSystem", tags = "商户系统")
@PreAuth(replace = "pay:merchantSystem:")
public class MerchantSystemController extends SuperController<MerchantSystemService, Long, MerchantSystem, MerchantSystemPageQuery, MerchantSystemSaveDTO, MerchantSystemUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<MerchantSystem> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<MerchantSystem> merchantSystemList = list.stream().map((map) -> {
            MerchantSystem merchantSystem = MerchantSystem.builder().build();
            //TODO 请在这里完成转换
            return merchantSystem;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(merchantSystemList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<MerchantSystemPageQuery> params) {
        MerchantSystemPageQuery pageQuery = params.getModel();

    }

    /**
     * @Author clm
     * @Description //分页查询
     * @Date 15:07 2023/2/22
     * @Param [params]
     * @return com.eastfair.core.base.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.eastfair.pay.vo.MerchantSystemVO>>
     **/
    @PostMapping("/queryList")
    @SysLog("商户分页查询")
    public R<Page<MerchantSystemVO>> queryList(@RequestBody PageParams<MerchantSystemPageQuery> params){
        try {
            return R.success(baseService.queryList(params));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    @PostMapping("/delete")
    @SysLog("删除商户")
    public R delete(@RequestBody MerchantSystem merchantSystem){
        try {
            baseService.delete(merchantSystem);
            return R.success(Boolean.TRUE);
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

}
